package com.chanzany.singleton.type6;


public class SingletonTest {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            new Thread(()->{
                Singleton instance = Singleton.getInstance();
            }).start();
        }
    }
}

/**
 * 懒汉式（双检锁）
 */
class Singleton{
    private Singleton(){}
    private static Singleton instance;
    public  static Singleton getInstance(){
        if (instance==null) {
            synchronized (Singleton.class){
                //双检锁
                if (instance==null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}
